#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <libarpc/axdr.h>
#include <libarpc/stack.h>

#include <atf-c.h>

#include "testobjs_xdr.h"

#ifndef __UNCONST
#define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
#endif


static axdr_ret_t atest_getlong(axdr_state_t *, int32_t *);
static axdr_ret_t atest_putlong(axdr_state_t *, const int32_t *);
static axdr_ret_t atest_getbytes(axdr_state_t *, char *, size_t);
static axdr_ret_t atest_putbytes(axdr_state_t *, const char *, size_t);
static void atest_destroy(axdr_state_t *);

typedef struct asyncobj_s {
	int         state;
	int         off;
	char        *buf;
	int         buflen;
} asyncobj_t;


static int
atest_read(void *arg, void *buf, size_t *lenp)
{
	asyncobj_t *obj;
	size_t len;

	obj = (asyncobj_t *)arg;

	if (obj->state < 2) {
		return EAGAIN;
	}

	len = *lenp;

	if (len <= 0) {
		return 0;
	}

	if (obj->off >= obj->buflen) {
		*lenp = 0;
		return 0;
	}

	obj->state = 0;
	memcpy(buf, &obj->buf[obj->off], 1);
	*lenp = 1;
	obj->off++;
	return 0;
}


static int
atest_write(void *arg, void *buf, size_t *lenp)
{
	asyncobj_t *obj;
	size_t len;
	char *tmp;
	int tlen;

	obj = (asyncobj_t *)arg;

	if (obj->state < 2) {
		return EAGAIN;
	}

	len = *lenp;

	if (len <= 0) {
		return 0;
	}

	if (obj->off >= obj->buflen) {
		tlen = obj->buflen + 1024;
		tmp = realloc(obj->buf, tlen);
		if (!tmp) {
			*lenp = 0;
			return EINVAL;
		}
		obj->buf = tmp;
		obj->buflen = tlen;
	}

	obj->state = 0;
	memcpy(&obj->buf[obj->off], buf, 1);
	*lenp = 1;
	obj->off++;
	return 0;
}

static axdr_ret_t
atest_getlong(axdr_state_t *xdr, int32_t *lp)
{
	axdr_ret_t rval;
	bool_t cleanup;
	char *buf;
	long val;
	size_t len;
	int err;
	int off;

	off = 0;
	rval = axdr_async_setup(xdr, &atest_getlong, &cleanup, &off,
				sizeof(*lp), (void**)&buf);
	if (rval != AXDR_DONE) {
		return rval;
	}

	len = sizeof(*lp) - off;
	err = atest_read(xdr->x_private, &buf[off], &len);
	if (err != 0) {
		if (err == EAGAIN) {
			rval = AXDR_WAITING;
		} else {
			rval = AXDR_ERROR;
		}
		axdr_async_teardown(xdr, &atest_getlong, off, cleanup, rval);
		return rval;
	}

	off += len;
	if (off >= sizeof(*lp)) {
		memcpy(&val, buf, sizeof(val));
		*lp = ntohl(val);
		rval = AXDR_DONE;
	} else {
		rval = AXDR_WAITING;
	}

	axdr_async_teardown(xdr, &atest_getlong, off, cleanup, rval);
	return rval;
}

static axdr_ret_t
atest_putlong(axdr_state_t *xdr, const int32_t *lp)
{
	axdr_ret_t rval;
	bool_t cleanup;
	long val;
	size_t len;
	int err;
	int off;

	off = 0;
	rval = axdr_async_setup(xdr, &atest_putlong, &cleanup, &off,
				0, (void **)NULL);
	if (rval != AXDR_DONE) {
		return rval;
	}

	val = htonl(*lp);

	len = sizeof(*lp) - off;
	err = atest_write(xdr->x_private, &((char *)&val)[off], &len);
	if (err != 0) {
		if (err == EAGAIN) {
			rval = AXDR_WAITING;
		} else {
			rval = AXDR_ERROR;
		}
		axdr_async_teardown(xdr, &atest_putlong, off, cleanup, rval);
		return rval;
	}

	off += len;
	if (off >= sizeof(*lp)) {
		rval = AXDR_DONE;
	} else {
		rval = AXDR_WAITING;
	}
	axdr_async_teardown(xdr, &atest_putlong, off, cleanup, rval);
	return rval;
}

static axdr_ret_t
atest_getbytes(axdr_state_t *xdr, char *buf, size_t blen)
{
	axdr_ret_t rval;
	bool_t cleanup;
	size_t len;
	int err;
	int off;

	off = 0;
	rval = axdr_async_setup(xdr, &atest_getbytes, &cleanup, &off,
				0, (void **)NULL);
	if (rval != AXDR_DONE) {
		return rval;
	}

	len = blen - off;
	err = atest_read(xdr->x_private, &buf[off], &len);
	if (err != 0) {
		if (err == EAGAIN) {
			rval = AXDR_WAITING;
		} else {
			rval = AXDR_ERROR;
		}
		axdr_async_teardown(xdr, &atest_getbytes, off, cleanup, rval);
		return rval;
	}

	off += len;
	if (off >= blen) {
		rval = AXDR_DONE;
	} else {
		rval = AXDR_WAITING;
	}

	axdr_async_teardown(xdr, &atest_getbytes, off, cleanup, rval);
	return rval;
}

static axdr_ret_t
atest_putbytes(axdr_state_t *xdr, const char *buf, size_t blen)
{
	axdr_ret_t rval;
	bool_t cleanup;
	size_t len;
	int err;
	int off;

	off = 0;
	rval = axdr_async_setup(xdr, &atest_putbytes, &cleanup, &off,
				0, (void **)NULL);
	if (rval != AXDR_DONE) {
		return rval;
	}

	len = blen - off;
	err = atest_write(xdr->x_private, __UNCONST(&buf[off]), &len);
	if (err != 0) {
		if (err == EAGAIN) {
			rval = AXDR_WAITING;
		} else {
			rval = AXDR_ERROR;
		}
		axdr_async_teardown(xdr, &atest_putbytes, off, cleanup, rval);
		return rval;
	}

	off += len;
	if (off >= blen) {
		rval = AXDR_DONE;
	} else {
		rval = AXDR_WAITING;
	}
	axdr_async_teardown(xdr, &atest_putbytes, off, cleanup, rval);
	return rval;
}

static void
atest_destroy(axdr_state_t *xdr)
{
	/* nothing to do, no state */
	return;
}

static struct axdr_ops_s aops = {
	&atest_getlong,
	&atest_putlong,
	&atest_getbytes,
	&atest_putbytes,
	NULL,
	NULL,
	NULL,
	&atest_destroy,
	NULL,
	NULL,
	NULL,
	NULL,
};

static void
_datadump(char *buf, size_t len)
{
	int i;
	for (i = 0; i < len; i++) {
		if (i != 0 && (i % 16) == 0) {
			printf("\n");
		}
		printf("%02hhx ", buf[i]);
	}
	printf("\n");
}

typedef int (*compare_t)(const void*, const void*, size_t);


static void
axdr_test_synchronous(axdrproc_t testfunc,
		      void *testobj, size_t size, compare_t cmp)
{
	size_t serialized_size;
	void *buf1, *buf2;
	axdr_ret_t rval;
	axdr_state_t src, dst;

	serialized_size = axdr_sizeof(testfunc, testobj);
	buf1 = malloc(serialized_size);
	ATF_REQUIRE(buf1 != NULL);

	/* encode */
	axdrmem_create(&src, (char*) buf1, serialized_size, AXDR_ENCODE);
	ATF_REQUIRE_MSG((rval = testfunc(&src, testobj)) == AXDR_DONE,
			"Unexpected encode return %d", rval);

	/* decode */
	axdrmem_create(&dst, (char *) buf1, serialized_size, AXDR_DECODE);
	buf2 = calloc(1, size);
	ATF_REQUIRE(buf2 != NULL);
	ATF_REQUIRE_MSG((rval = testfunc(&dst, buf2)) == AXDR_DONE,
			"Unexpected decode return %d", rval);
	ATF_REQUIRE(cmp(testobj, buf2, size) == 0);
	axdr_free(testfunc, buf2);
	free(buf2);

	_datadump(buf1, serialized_size);
	free(buf1);
}

static void
axdrrec_test_asyncrec(axdrproc_t testfunc, void *testobj,
		      size_t size, compare_t cmp, int size1, int size2,
		      bool_t log)
{
	size_t serialized_size;
	void *result;
	astk_t stack;
	asyncobj_t obj;
	axdr_ret_t ret;
	axdr_state_t xdr;
	int err;

	serialized_size = axdr_sizeof(testfunc, testobj);

	ATF_REQUIRE_MSG((err = axdrrec_create(&xdr, size1, size2, &obj,
					      &atest_read, &atest_write)) == 0,
			"unable to create src xrec err=%d", err);

	ATF_REQUIRE_MSG((err = astk_init(&stack)) == 0,
			"unable to create async stack err=%d", err);

	xdr.x_async = &stack;

	obj.state = 1;
	obj.off = 0;
	obj.buflen = serialized_size;
	obj.buf = malloc(serialized_size);
	ATF_REQUIRE(obj.buf != NULL);

	xdr.x_op = AXDR_ENCODE_ASYNC;

	/* encode it first */
	for (;;) {
		ret = (*testfunc)(&xdr, testobj);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"encode error return=%d", ret);
	}

	/* flush it */
	for (;;) {
		ret = axdrrec_endofrecord(&xdr, TRUE);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"flush error return=%d", ret);
	}

	if (log) {
		printf("encoded (%d/%d bytes)\n", obj.off,
		       (int32_t) serialized_size);
		_datadump(obj.buf, obj.off);
	}

	result = malloc(size);
	ATF_REQUIRE(result != NULL);
	memset(result, 0, size);

	xdr.x_op = AXDR_DECODE_ASYNC;

	obj.state = 0;
	obj.buflen = obj.off;
	obj.off = 0;

	for (;;) {
		ret = axdrrec_skiprecord(&xdr);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"skip error return=%d", ret);
	}
	for (;;) {
		ret = (*testfunc)(&xdr, result);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"decode error return=%d", ret);
	}

	free(obj.buf);
	axdr_destroy(&xdr);
	astk_cleanup(&stack);

	ATF_REQUIRE_MSG(cmp(testobj, result, size) == 0,
			"objects don't match");
	axdr_free(testfunc, result);
	free(result);
}


static void
axdrrec_test_async(axdrproc_t testfunc, void *testobj,
		   size_t size, compare_t cmp)
{
	size_t serialized_size;
	void *result;
	astk_t stack;
	asyncobj_t obj;
	axdr_ret_t ret;
	axdr_state_t xdr;
	int err;

	serialized_size = axdr_sizeof(testfunc, testobj);

	memset(&xdr, 0, sizeof(xdr));
	xdr.x_op = AXDR_ENCODE_ASYNC;
	xdr.x_ops = &aops;
	xdr.x_private = &obj;

	ATF_REQUIRE_MSG((err = astk_init(&stack)) == 0,
			"unable to create async stack err=%d", err);
	xdr.x_async = &stack;

	obj.state = 0;
	obj.off = 0;
	obj.buflen = serialized_size;
	obj.buf = malloc(serialized_size);
	ATF_REQUIRE(obj.buf != NULL);

	xdr.x_op = AXDR_ENCODE_ASYNC;

	/* encode it first */
	for (;;) {
		ret = (*testfunc)(&xdr, testobj);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"encode error return=%d", ret);
	}

	ATF_REQUIRE_MSG(obj.off == serialized_size,
			"encode length mismatch %d/%zu",
			obj.off, serialized_size);

	printf("encoded\n");
	_datadump(obj.buf, obj.off);

	result = malloc(size);
	ATF_REQUIRE(result != NULL);
	memset(result, 0, size);

	xdr.x_op = AXDR_DECODE_ASYNC;

	obj.state = 0;
	obj.buflen = obj.off;
	obj.off = 0;

	for (;;) {
		ret = (*testfunc)(&xdr, result);
		if (ret == AXDR_DONE) {
			break;
		}

		obj.state++;
		ATF_REQUIRE_MSG(ret == AXDR_WAITING,
				"decode error return=%d", ret);
	}

	free(obj.buf);
	astk_cleanup(&stack);

	ATF_REQUIRE_MSG(cmp(testobj, result, size) == 0,
			"Objects don't match.");
	axdr_free(testfunc, result);
	free(result);
}

static void
axdr_test_print(axdrproc_t testfunc, void *testobj)
{
	axdr_state_t xdr;

	/* testout stringify */
	ATF_REQUIRE(axdr_fprint_create(&xdr, stdout, "axdr_test:") == 0);
	ATF_REQUIRE((*testfunc)(&xdr, testobj) == AXDR_DONE);
	axdr_destroy(&xdr);
}

static void
axdr_test(const char *name, axdrproc_t testfunc,
	  void *testobj, size_t size, compare_t cmp)
{
	printf("%s: print:\n", name);
	axdr_test_print(testfunc, testobj);
	printf("%s: sync:\n", name);
	axdr_test_synchronous(testfunc, testobj, size, cmp);
	printf("\n%s: async:\n", name);
	axdrrec_test_async(testfunc, testobj, size, cmp);
	printf("\n%s: async record:\n", name);
	axdrrec_test_asyncrec(testfunc, testobj, size,
			      cmp, 2000, 2000, TRUE);
	printf("\n");
}

static axdr_ret_t
axdr_encap_int(axdr_state_t *xdrs, int *val)
{
	return axdr_encap(xdrs, (axdrproc_t) axdr_int, val);
}

static int
string_s_cmp(string_s *a, string_s *b, int size)
{
	return strcmp(a->str, b->str);
}

static int
test_u_cmp(test_u *a, test_u *b, int size)
{
#define len test_u_u.string_s_list.string_s_list_len
#define val test_u_u.string_s_list.string_s_list_val
	int i;

	if (a->which != b->which)
		return 1;
	if (a->len != b->len)
		return 1;
	for (i = 0; i < a->len; i++) {
		if (string_s_cmp(&a->val[i], &b->val[i], -1) != 0)
			return 1;
	}
	return 0;
#undef len
#undef val
}

ATF_TC_WITHOUT_HEAD(test_xdr_types);
ATF_TC_BODY(test_xdr_types, tc)
{
	int i = 42;
	quad_t q = -8589934592LL;
	bool_t b = TRUE;
	short s = -23;
	string_s str_s = { .str = __UNCONST("foobar") };
	test_u my_test_u = {
		.which = 8,
		.test_u_u = { .string_s_list = {
				 .string_s_list_len = 1,
				 .string_s_list_val = &str_s }
		}
	};

	axdr_test("axdr_int", (axdrproc_t) axdr_int,
		  &i, sizeof(i), memcmp);
	axdr_test("axdr_hyper", (axdrproc_t) axdr_hyper,
		  &q, sizeof(q), memcmp);
	axdr_test("axdr_bool", (axdrproc_t) axdr_bool,
		  &b, sizeof(b), memcmp);
	axdr_test("axdr_short", (axdrproc_t) axdr_short,
		  &s, sizeof(s), memcmp);
	axdr_test("axdr_encap_int", (axdrproc_t) axdr_encap_int,
		  &i, sizeof(i), memcmp);
	axdr_test("axdr_string_s", (axdrproc_t) axdr_string_s,
		  &str_s, sizeof(string_s), (compare_t) string_s_cmp);
	axdr_test("axdr_test_u", (axdrproc_t) axdr_test_u,
		  &my_test_u, sizeof(test_u), (compare_t) test_u_cmp);
}

ATF_TC_WITHOUT_HEAD(test_xdr_opaque);

struct my_opaque {
	char bytes[8];
};

static axdr_ret_t
axdr_my_opaque(axdr_state_t *xdrs, struct my_opaque *objp)
{
	return axdr_opaque(xdrs, objp->bytes, 8);
}

static axdr_ret_t
axdr_my_opaque_1(axdr_state_t *xdrs, struct my_opaque *objp)
{
	return axdr_opaque(xdrs, objp->bytes, 1);
}

static axdr_ret_t
axdr_my_opaque_2(axdr_state_t *xdrs, struct my_opaque *objp)
{
	return axdr_opaque(xdrs, objp->bytes, 2);
}

static axdr_ret_t
axdr_my_opaque_5(axdr_state_t *xdrs, struct my_opaque *objp)
{
	return axdr_opaque(xdrs, objp->bytes, 5);
}

ATF_TC_BODY(test_xdr_opaque, tc)
{
	char bytes[] = { 0xde, 0xad, 0xbe, 0xef, 0xfa, 0xda, 0xba, 0xaa };

	axdr_test("axdr_opaque", (axdrproc_t) axdr_my_opaque,
		  bytes, sizeof(bytes), (compare_t) memcmp);
	axdr_test("axdr_opaque_1", (axdrproc_t) axdr_my_opaque_1,
		  bytes, 1, (compare_t) memcmp);
	axdr_test("axdr_opaque_2", (axdrproc_t) axdr_my_opaque_2,
		  bytes, 2, (compare_t) memcmp);
	axdr_test("axdr_opaque_5", (axdrproc_t) axdr_my_opaque_5,
		  bytes, 5, (compare_t) memcmp);
}

ATF_TC_WITHOUT_HEAD(test_xdr_complex);

static unsigned char pkgstatus_blob[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x07, 0x63, 0x75, 0x72, 0x72,
	0x65, 0x6e, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x05,
	0x39, 0x38, 0x30, 0x31, 0x38, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x0f,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
	0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x28,
	0x37, 0x32, 0x34, 0x65, 0x32, 0x31, 0x62, 0x63,
	0x32, 0x61, 0x35, 0x34, 0x38, 0x38, 0x31, 0x34,
	0x64, 0x35, 0x63, 0x61, 0x31, 0x62, 0x39, 0x32,
	0x61, 0x38, 0x39, 0x64, 0x62, 0x64, 0x66, 0x62,
	0x35, 0x33, 0x62, 0x36, 0x61, 0x31, 0x35, 0x38,
	0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x7b, 0x5a, 0x00, 0x00, 0x00, 0x06,
	0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0a,
	0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x32,
	0x73, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
	0x75, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
	0x00, 0x00, 0x00, 0x02, 0x75, 0x69, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x07,
	0x62, 0x61, 0x73, 0x65, 0x5f, 0x75, 0x69, 0x00,
	0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x05, 0x39, 0x38, 0x32, 0x32,
	0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2c,
	0x00, 0x00, 0x00, 0x28, 0x34, 0x64, 0x36, 0x34,
	0x31, 0x63, 0x33, 0x61, 0x38, 0x63, 0x63, 0x32,
	0x37, 0x63, 0x65, 0x34, 0x32, 0x34, 0x30, 0x63,
	0x64, 0x65, 0x31, 0x33, 0x39, 0x65, 0x63, 0x61,
	0x62, 0x33, 0x63, 0x34, 0x34, 0x34, 0x30, 0x37,
	0x35, 0x37, 0x31, 0x64, 0x00, 0x00, 0x00, 0x0a,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x7b, 0xb5,
	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x75, 0x69, 0x2e, 0x32,
	0x73, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06,
	0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
	0x00, 0x00, 0x00, 0x04, 0x6c, 0x61, 0x6e, 0x67,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04,
	0x6c, 0x61, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x09, 0x63, 0x6f, 0x6d, 0x6d,
	0x6f, 0x6e, 0x5f, 0x65, 0x6e, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x05, 0x39, 0x38, 0x32, 0x32,
	0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2c,
	0x00, 0x00, 0x00, 0x28, 0x36, 0x31, 0x30, 0x35,
	0x34, 0x62, 0x64, 0x39, 0x62, 0x36, 0x64, 0x34,
	0x63, 0x35, 0x64, 0x36, 0x36, 0x35, 0x63, 0x38,
	0x38, 0x66, 0x33, 0x36, 0x35, 0x33, 0x61, 0x62,
	0x62, 0x34, 0x65, 0x36, 0x31, 0x38, 0x37, 0x35,
	0x35, 0x61, 0x37, 0x39, 0x00, 0x00, 0x00, 0x0a,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0xdc, 0xd8,
	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18,
	0x00, 0x00, 0x00, 0x12, 0x63, 0x6f, 0x6d, 0x6d,
	0x6f, 0x6e, 0x5f, 0x65, 0x6e, 0x2e, 0x6c, 0x61,
	0x6e, 0x67, 0x2e, 0x32, 0x73, 0x70, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x02, 0x75, 0x69, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x07, 0x70, 0x65, 0x6e, 0x64,
	0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x73, 0x79, 0x73, 0x74,
	0x65, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x05,
	0x39, 0x38, 0x30, 0x31, 0x38, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x0f,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
	0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x28,
	0x37, 0x32, 0x34, 0x65, 0x32, 0x31, 0x62, 0x63,
	0x32, 0x61, 0x35, 0x34, 0x38, 0x38, 0x31, 0x34,
	0x64, 0x35, 0x63, 0x61, 0x31, 0x62, 0x39, 0x32,
	0x61, 0x38, 0x39, 0x64, 0x62, 0x64, 0x66, 0x62,
	0x35, 0x33, 0x62, 0x36, 0x61, 0x31, 0x35, 0x38,
	0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x7b, 0x5a, 0x00, 0x00, 0x00, 0x06,
	0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0a,
	0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x32,
	0x73, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
	0x75, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
	0x00, 0x00, 0x00, 0x02, 0x75, 0x69, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x07,
	0x62, 0x61, 0x73, 0x65, 0x5f, 0x75, 0x69, 0x00,
	0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x05, 0x39, 0x38, 0x32, 0x32,
	0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2c,
	0x00, 0x00, 0x00, 0x28, 0x34, 0x64, 0x36, 0x34,
	0x31, 0x63, 0x33, 0x61, 0x38, 0x63, 0x63, 0x32,
	0x37, 0x63, 0x65, 0x34, 0x32, 0x34, 0x30, 0x63,
	0x64, 0x65, 0x31, 0x33, 0x39, 0x65, 0x63, 0x61,
	0x62, 0x33, 0x63, 0x34, 0x34, 0x34, 0x30, 0x37,
	0x35, 0x37, 0x31, 0x64, 0x00, 0x00, 0x00, 0x0a,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x7b, 0xb5,
	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x06, 0x75, 0x69, 0x2e, 0x32,
	0x73, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06,
	0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
	0x00, 0x00, 0x00, 0x04, 0x6c, 0x61, 0x6e, 0x67,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04,
	0x6c, 0x61, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x09, 0x63, 0x6f, 0x6d, 0x6d,
	0x6f, 0x6e, 0x5f, 0x65, 0x6e, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c,
	0x00, 0x00, 0x00, 0x05, 0x39, 0x38, 0x32, 0x32,
	0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2c,
	0x00, 0x00, 0x00, 0x28, 0x36, 0x31, 0x30, 0x35,
	0x34, 0x62, 0x64, 0x39, 0x62, 0x36, 0x64, 0x34,
	0x63, 0x35, 0x64, 0x36, 0x36, 0x35, 0x63, 0x38,
	0x38, 0x66, 0x33, 0x36, 0x35, 0x33, 0x61, 0x62,
	0x62, 0x34, 0x65, 0x36, 0x31, 0x38, 0x37, 0x35,
	0x35, 0x61, 0x37, 0x39, 0x00, 0x00, 0x00, 0x0a,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0xdc, 0xd8,
	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18,
	0x00, 0x00, 0x00, 0x12, 0x63, 0x6f, 0x6d, 0x6d,
	0x6f, 0x6e, 0x5f, 0x65, 0x6e, 0x2e, 0x6c, 0x61,
	0x6e, 0x67, 0x2e, 0x32, 0x73, 0x70, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
	0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x02, 0x75, 0x69, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x10,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03,
	0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x28,
	0x00, 0x00, 0x00, 0x23, 0x68, 0x74, 0x74, 0x70,
	0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31,
	0x36, 0x38, 0x2e, 0x31, 0x2e, 0x35, 0x2f, 0x75,
	0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x5f, 0x77,
	0x69, 0x66, 0x69, 0x2e, 0x32, 0x73, 0x70, 0x00,
	0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x18,
	0x00, 0x00, 0x00, 0x14, 0x66, 0x61, 0x69, 0x6c,
	0x69, 0x6e, 0x67, 0x20, 0x61, 0x66, 0x74, 0x65,
	0x72, 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f, 0x74,
	0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00
};

static int
pkgstatus_cmp(pkgstatusr *a, pkgstatusr *b, int size)
{
	axdr_ret_t ret;
	axdr_state_t xdr;
	int len;
	char *buf;

	len = sizeof(pkgstatus_blob);
	buf = malloc(len);
	if (!buf) {
		return 1;
	}

	memset(buf, 0, len);

	axdrmem_create(&xdr, buf, len, AXDR_ENCODE);
	ret = axdr_pkgstatusr(&xdr, a);
	axdr_destroy(&xdr);

	if (ret != AXDR_DONE || memcmp(buf, pkgstatus_blob, len) != 0) {
		free(buf);
		return 1;
	}

	memset(buf, 0, len);

	axdrmem_create(&xdr, buf, len, AXDR_ENCODE);
	ret = axdr_pkgstatusr(&xdr, b);
	axdr_destroy(&xdr);

	if (ret != AXDR_DONE || memcmp(buf, pkgstatus_blob, len) != 0) {
		free(buf);
		return 1;
	}

	free(buf);
	return 0;
}

ATF_TC_BODY(test_xdr_complex, tc)
{
	axdr_ret_t ret;
	axdr_state_t xdr;
	pkgstatusr statr;

	memset(&statr, 0, sizeof(statr));

	/* test pkgstatus */
	axdrmem_create(&xdr, (char *) pkgstatus_blob,
		       sizeof(pkgstatus_blob), AXDR_DECODE);
	ret = axdr_pkgstatusr(&xdr, &statr);
	ATF_REQUIRE(ret != AXDR_ERROR);
	if (ret == AXDR_DONE) {
		int j;

		axdr_test("axdr_pkgstatusr",
			  (axdrproc_t)axdr_pkgstatusr, &statr,
			  sizeof(statr), (compare_t) pkgstatus_cmp);
		printf("\ntesting various record block sizes:\n");

		for (j = 100; j < 65000; ) {
			axdrrec_test_asyncrec((axdrproc_t)axdr_pkgstatusr,
					      &statr, sizeof(statr),
					      (compare_t) pkgstatus_cmp,
					      2000, j, FALSE);
			axdrrec_test_asyncrec((axdrproc_t)axdr_pkgstatusr,
					      &statr, sizeof(statr),
					      (compare_t) pkgstatus_cmp,
					      j, 2000, FALSE);
			if (j < 200) {
				j += 8;
			} else {
				j += j / 4;
			}
		}
	}
	axdr_free((axdrproc_t)axdr_pkgstatusr, &statr);
	axdr_destroy(&xdr);
}


ATF_TC_WITHOUT_HEAD(test_xdr_pointers);

static unsigned char op_entry_blob[] = {
	0x6b, 0x8b, 0x45, 0x67, 0x00, 0x00, 0x00, 0x01,
	0x64, 0x3c, 0x98, 0x69, 0x00, 0x00, 0x00, 0x01,
	0x74, 0xb0, 0xdc, 0x51, 0x00, 0x00, 0x00, 0x01,
	0x2a, 0xe8, 0x94, 0x4a, 0x00, 0x00, 0x00, 0x01,
	0x23, 0x8e, 0x1f, 0x29, 0x00, 0x00, 0x00, 0x01,
	0x3d, 0x1b, 0x58, 0xba, 0x00, 0x00, 0x00, 0x01,
	0x2e, 0xb1, 0x41, 0xf2, 0x00, 0x00, 0x00, 0x01,
	0x79, 0xe2, 0xa9, 0xe3, 0x00, 0x00, 0x00, 0x01,
	0x51, 0x5f, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x01,
	0x12, 0x20, 0x08, 0x54, 0x00, 0x00, 0x00, 0x01,
	0x02, 0x16, 0x23, 0x1b, 0x00, 0x00, 0x00, 0x01,
	0x11, 0x90, 0xcd, 0xe7, 0x00, 0x00, 0x00, 0x01,
	0x14, 0x0e, 0x0f, 0x76, 0x00, 0x00, 0x00, 0x01,
	0x10, 0x9c, 0xf9, 0x2e, 0x00, 0x00, 0x00, 0x01,
	0x7f, 0xdc, 0xc2, 0x33, 0x00, 0x00, 0x00, 0x01,
	0x41, 0xa7, 0xc4, 0xc9, 0x00, 0x00, 0x00, 0x01,
	0x4e, 0x6a, 0xfb, 0x66, 0x00, 0x00, 0x00, 0x01,
	0x51, 0x9b, 0x50, 0x0d, 0x00, 0x00, 0x00, 0x01,
	0x3f, 0x2d, 0xba, 0x31, 0x00, 0x00, 0x00, 0x01,
	0x25, 0x71, 0x30, 0xa3, 0x00, 0x00, 0x00, 0x01,
	0x43, 0x6c, 0x61, 0x25, 0x00, 0x00, 0x00, 0x01,
	0x33, 0x3a, 0xb1, 0x05, 0x00, 0x00, 0x00, 0x01,
	0x24, 0x43, 0xa8, 0x58, 0x00, 0x00, 0x00, 0x01,
	0x67, 0x63, 0x84, 0x5e, 0x00, 0x00, 0x00, 0x01,
	0x08, 0xed, 0xbd, 0xab, 0x00, 0x00, 0x00, 0x01,
	0x43, 0x53, 0xd0, 0xcd, 0x00, 0x00, 0x00, 0x01,
	0x18, 0x9a, 0x76, 0x9b, 0x00, 0x00, 0x00, 0x01,
	0x71, 0xf3, 0x24, 0x54, 0x00, 0x00, 0x00, 0x01,
	0x08, 0x36, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x01,
	0x3a, 0x95, 0xf8, 0x74, 0x00, 0x00, 0x00, 0x01,
	0x1e, 0x7f, 0xf5, 0x21, 0x00, 0x00, 0x00, 0x01,
	0x73, 0x7b, 0x8d, 0xdc, 0x00, 0x00, 0x00, 0x01,
	0x22, 0x22, 0x1a, 0x70, 0x00, 0x00, 0x00, 0x01,
	0x30, 0x06, 0xc8, 0x3e, 0x00, 0x00, 0x00, 0x01,
	0x41, 0x9a, 0xc2, 0x41, 0x00, 0x00, 0x00, 0x01,
	0x44, 0x0b, 0xad, 0xfc, 0x00, 0x00, 0x00, 0x01,
	0x38, 0x04, 0x82, 0x3e, 0x00, 0x00, 0x00, 0x01,
	0x77, 0x24, 0xc6, 0x7e, 0x00, 0x00, 0x00, 0x01,
	0x24, 0x63, 0xb9, 0xea, 0x00, 0x00, 0x00, 0x01,
	0x51, 0xea, 0xd3, 0x6b, 0x00, 0x00, 0x00, 0x01,
	0x58, 0x0b, 0xd7, 0x8f, 0x00, 0x00, 0x00, 0x01,
	0x38, 0x55, 0x58, 0x5c, 0x00, 0x00, 0x00, 0x01,
	0x6a, 0x23, 0x42, 0xec, 0x00, 0x00, 0x00, 0x01,
	0x1d, 0x4e, 0xd4, 0x3b, 0x00, 0x00, 0x00, 0x01,
	0x2c, 0xd8, 0x9a, 0x32, 0x00, 0x00, 0x00, 0x01,
	0x7a, 0x6d, 0x8d, 0x3c, 0x00, 0x00, 0x00, 0x01,
	0x54, 0x22, 0x89, 0xec, 0x00, 0x00, 0x00, 0x01,
	0x38, 0x43, 0x7f, 0xdb, 0x00, 0x00, 0x00, 0x01,
	0x32, 0xff, 0xf9, 0x02, 0x00, 0x00, 0x00, 0x01,
	0x57, 0x94, 0x78, 0xfe, 0x00, 0x00, 0x00, 0x01,
	0x3d, 0xc2, 0x40, 0xfb, 0x00, 0x00, 0x00, 0x01,
	0x79, 0xa1, 0xde, 0xaa, 0x00, 0x00, 0x00, 0x01,
	0x12, 0xe6, 0x85, 0xfb, 0x00, 0x00, 0x00, 0x01,
	0x52, 0x0e, 0xed, 0xd1, 0x00, 0x00, 0x00, 0x01,
	0x4f, 0x4e, 0xf0, 0x05, 0x00, 0x00, 0x00, 0x01,
	0x64, 0x9b, 0xb7, 0x7c, 0x00, 0x00, 0x00, 0x01,
	0x39, 0x38, 0x65, 0x75, 0x00, 0x00, 0x00, 0x01,
	0x18, 0x01, 0x15, 0xbe, 0x00, 0x00, 0x00, 0x01,
	0x47, 0x39, 0x8c, 0x89, 0x00, 0x00, 0x00, 0x01,
	0x15, 0xb5, 0xaf, 0x5c, 0x00, 0x00, 0x00, 0x01,
	0x0d, 0x34, 0xb6, 0xa8, 0x00, 0x00, 0x00, 0x01,
	0x3f, 0x6a, 0xb6, 0x0f, 0x00, 0x00, 0x00, 0x01,
	0x7e, 0x0c, 0x57, 0xb1, 0x00, 0x00, 0x00, 0x01,
	0x57, 0x9b, 0xe4, 0xf1, 0x00, 0x00, 0x00, 0x01,
	0x5f, 0xf8, 0x7e, 0x05, 0x00, 0x00, 0x00, 0x01,
	0x25, 0xa7, 0x0b, 0xf7, 0x00, 0x00, 0x00, 0x01,
	0x4a, 0xd0, 0x84, 0xe9, 0x00, 0x00, 0x00, 0x01,
	0x13, 0x81, 0x82, 0x3a, 0x00, 0x00, 0x00, 0x01,
	0x10, 0x0f, 0x8f, 0xca, 0x00, 0x00, 0x00, 0x01,
	0x15, 0x01, 0x4a, 0xcb, 0x00, 0x00, 0x00, 0x01,
	0x09, 0x8a, 0x31, 0x48, 0x00, 0x00, 0x00, 0x01,
	0x06, 0xb9, 0x47, 0x64, 0x00, 0x00, 0x00, 0x01,
	0x16, 0x8e, 0x12, 0x1f, 0x00, 0x00, 0x00, 0x01,
	0x66, 0x1e, 0x3f, 0x1e, 0x00, 0x00, 0x00, 0x01,
	0x54, 0x0a, 0x47, 0x1c, 0x00, 0x00, 0x00, 0x01,
	0x51, 0xd9, 0xc5, 0x64, 0x00, 0x00, 0x00, 0x01,
	0x0b, 0xf7, 0x2b, 0x14, 0x00, 0x00, 0x00, 0x01,
	0x42, 0x96, 0x3e, 0x5a, 0x00, 0x00, 0x00, 0x01,
	0x08, 0xf2, 0xb1, 0x5e, 0x00, 0x00, 0x00, 0x01,
	0x3b, 0x0f, 0xd3, 0x79, 0x00, 0x00, 0x00, 0x01,
	0x49, 0x62, 0x81, 0x3b, 0x00, 0x00, 0x00, 0x01,
	0x06, 0xa5, 0xee, 0x64, 0x00, 0x00, 0x00, 0x01,
	0x7f, 0xff, 0xca, 0x11, 0x00, 0x00, 0x00, 0x01,
	0x71, 0xea, 0x11, 0x09, 0x00, 0x00, 0x00, 0x01,
	0x7f, 0xb7, 0xe0, 0xaa, 0x00, 0x00, 0x00, 0x01,
	0x6f, 0x6d, 0xd9, 0xac, 0x00, 0x00, 0x00, 0x01,
	0x00, 0x88, 0x5e, 0x1b, 0x00, 0x00, 0x00, 0x01,
	0x4c, 0x04, 0xa8, 0xaf, 0x00, 0x00, 0x00, 0x01,
	0x14, 0xe1, 0x7e, 0x33, 0x00, 0x00, 0x00, 0x01,
	0x74, 0xde, 0x0e, 0xe3, 0x00, 0x00, 0x00, 0x01,
	0x2d, 0xf6, 0xd6, 0x48, 0x00, 0x00, 0x00, 0x01,
	0x4a, 0x2a, 0xc3, 0x15, 0x00, 0x00, 0x00, 0x01,
	0x57, 0xfc, 0x4f, 0xbb, 0x00, 0x00, 0x00, 0x01,
	0x43, 0xf1, 0x84, 0x22, 0x00, 0x00, 0x00, 0x01,
	0x26, 0xf3, 0x24, 0xba, 0x00, 0x00, 0x00, 0x01,
	0x49, 0xda, 0x30, 0x7d, 0x00, 0x00, 0x00, 0x01,
	0x5f, 0xb8, 0x37, 0x0b, 0x00, 0x00, 0x00, 0x01,
	0x04, 0x88, 0xac, 0x1a, 0x00, 0x00, 0x00, 0x01,
	0x6a, 0xa7, 0x8f, 0x7f, 0x00, 0x00, 0x00, 0x01,
	0x6f, 0xc7, 0x5a, 0xf8, 0x00, 0x00, 0x00, 0x01,
	0x7d, 0x5e, 0x18, 0xf8, 0x00, 0x00, 0x00, 0x01,
	0x73, 0xa1, 0x82, 0x1b, 0x00, 0x00, 0x00, 0x01,
	0x55, 0x5c, 0x55, 0xb5, 0x00, 0x00, 0x00, 0x01,
	0x14, 0xfc, 0xe7, 0x4e, 0x00, 0x00, 0x00, 0x01,
	0x71, 0xc9, 0x12, 0x98, 0x00, 0x00, 0x00, 0x01,
	0x53, 0x29, 0x99, 0x38, 0x00, 0x00, 0x00, 0x01,
	0x50, 0x92, 0xca, 0x79, 0x00, 0x00, 0x00, 0x01,
	0x59, 0xad, 0xea, 0x3d, 0x00, 0x00, 0x00, 0x01,
	0x2a, 0x15, 0x5d, 0xbc, 0x00, 0x00, 0x00, 0x01,
	0x09, 0x7e, 0x1b, 0x4e, 0x00, 0x00, 0x00, 0x01,
	0x1c, 0xa0, 0xc5, 0xfa, 0x00, 0x00, 0x00, 0x01,
	0x41, 0x5e, 0x28, 0x6c, 0x00, 0x00, 0x00, 0x01,
	0x23, 0xd8, 0x6a, 0xac, 0x00, 0x00, 0x00, 0x01,
	0x5c, 0x10, 0xfe, 0x21, 0x00, 0x00, 0x00, 0x01,
	0x3c, 0x59, 0x91, 0xaa, 0x00, 0x00, 0x00, 0x01,
	0x78, 0xdf, 0x6a, 0x55, 0x00, 0x00, 0x00, 0x01,
	0x2b, 0x0d, 0x8d, 0xbe, 0x00, 0x00, 0x00, 0x01,
	0x37, 0x9e, 0x21, 0xb5, 0x00, 0x00, 0x00, 0x01,
	0x2c, 0x27, 0x17, 0x3b, 0x00, 0x00, 0x00, 0x01,
	0x6a, 0xa7, 0xb7, 0x5c, 0x00, 0x00, 0x00, 0x01,
	0x56, 0x75, 0xff, 0x36, 0x00, 0x00, 0x00, 0x01,
	0x3d, 0xb0, 0x12, 0xb3, 0x00, 0x00, 0x00, 0x01,
	0x5b, 0x25, 0xac, 0xe2, 0x00, 0x00, 0x00, 0x01,
	0x4f, 0x97, 0xe3, 0xe4, 0x00, 0x00, 0x00, 0x01,
	0x34, 0xfd, 0x6b, 0x4f, 0x00, 0x00, 0x00, 0x01,
	0x56, 0x43, 0x8d, 0x15, 0x00, 0x00, 0x00, 0x01,
	0x2c, 0x6e, 0x4a, 0xfd, 0x00, 0x00, 0x00, 0x01,
	0x4d, 0xf7, 0x2e, 0x4e, 0x00, 0x00, 0x00, 0x01,
	0x5d, 0x88, 0x8a, 0x08, 0x00, 0x00, 0x00, 0x01,
	0x5e, 0xc6, 0xaf, 0xd4, 0x00, 0x00, 0x00, 0x01,
	0x75, 0xe0, 0x85, 0x8a, 0x00, 0x00, 0x00, 0x01,
	0x53, 0x99, 0xc6, 0x54, 0x00, 0x00, 0x00, 0x01,
	0x44, 0x27, 0x06, 0x9a, 0x00, 0x00, 0x00, 0x01,
	0x21, 0x57, 0xf6, 0xbc, 0x00, 0x00, 0x00, 0x01,
	0x57, 0xd2, 0xf1, 0x0e, 0x00, 0x00, 0x00, 0x01,
	0x0e, 0x3e, 0x47, 0xa8, 0x00, 0x00, 0x00, 0x01,
	0x49, 0xd0, 0xfe, 0xac, 0x00, 0x00, 0x00, 0x01,
	0x55, 0x51, 0xb9, 0xf3, 0x00, 0x00, 0x00, 0x01,
	0x63, 0x4c, 0x57, 0x4c, 0x00, 0x00, 0x00, 0x01,
	0x2a, 0x31, 0xb6, 0x2d, 0x00, 0x00, 0x00, 0x01,
	0x7d, 0xff, 0x9d, 0x09, 0x00, 0x00, 0x00, 0x01,
	0x69, 0xe7, 0xf3, 0xe5, 0x00, 0x00, 0x00, 0x01,
	0x18, 0x16, 0xf8, 0xc4, 0x00, 0x00, 0x00, 0x01,
	0x7a, 0xb4, 0x9d, 0xaf, 0x00, 0x00, 0x00, 0x01,
	0x61, 0xe7, 0x4e, 0xa3, 0x00, 0x00, 0x00, 0x01,
	0x0f, 0x81, 0x9e, 0x7f, 0x00, 0x00, 0x00, 0x01,
	0x31, 0x21, 0x67, 0xad, 0x00, 0x00, 0x00, 0x01,
	0x78, 0xb5, 0xe7, 0x76, 0x00, 0x00, 0x00, 0x01,
	0x6e, 0x53, 0x4c, 0xde, 0x00, 0x00, 0x00, 0x01,
	0x65, 0x96, 0x8c, 0x1c, 0x00, 0x00, 0x00, 0x00,
	0x46, 0x26, 0x3d, 0xec, 0x1a, 0x0d, 0xde, 0x32,
	0x75, 0x48, 0x6e, 0x47, 0x63, 0x1b, 0x64, 0xd4,
	0x57, 0xc7, 0xd4, 0x2d, 0x59, 0x7b, 0x4d, 0x84,
	0x75, 0x9f, 0x82, 0xcd, 0x37, 0xdf, 0x22, 0x33,
	0x2a, 0x6d, 0xe8, 0x06, 0x00, 0x75, 0x43, 0x42,
	0x18, 0x49, 0xc2, 0x9b, 0x24, 0xe9, 0x9d, 0xd7,
	0x24, 0xf6, 0xab, 0x8e, 0x4b, 0xee, 0x5a, 0x5b,
	0x2e, 0x48, 0xf0, 0x44, 0x0b, 0xff, 0xae, 0x18,
	0x70, 0x4e, 0x1d, 0xd5, 0x0b, 0x37, 0xe8, 0x0a,
	0x20, 0xee, 0x13, 0x48, 0x57, 0xa6, 0x1a, 0x29,
	0x19, 0xe2, 0x1b, 0xb2, 0x2a, 0x08, 0x2c, 0x70,
	0x50, 0x46, 0xb5, 0xa9, 0x17, 0xa1, 0xb5, 0x82,
	0x51, 0x9e, 0x31, 0x49, 0x59, 0x15, 0xff, 0x32,
	0x05, 0x3b, 0x0a, 0x9e, 0x17, 0x5d, 0xfc, 0xf0,
	0x27, 0x08, 0xc9, 0xaf, 0x3d, 0xd1, 0x50, 0x94,
	0x1d, 0xf0, 0x29, 0xd3, 0x4c, 0x9b, 0x09, 0x04,
	0x00, 0x69, 0xe3, 0x73, 0x6c, 0x80, 0xec, 0x70,
	0x39, 0xb7, 0xaa, 0xa2, 0x4b, 0xd8, 0x59, 0x1a,
	0x0e, 0x7f, 0xfa, 0x2b, 0x45, 0xe6, 0xd4, 0x86,
	0x7c, 0x58, 0xfd, 0x05, 0x53, 0x58, 0x4b, 0xcb,
	0x51, 0x08, 0x82, 0x77, 0x1d, 0x9f, 0x6e, 0x5f,
	0x28, 0x8f, 0x1a, 0x34, 0x1d, 0x54, 0x5c, 0x4d,
	0x1f, 0xbf, 0xe8, 0xe0, 0x09, 0xda, 0xf6, 0x32,
	0x6a, 0x3d, 0xd3, 0xe8, 0x3f, 0xa6, 0x2a, 0xca,
	0x7d, 0xe6, 0x77, 0x13, 0x5f, 0x35, 0x34, 0xa4,
	0x6a, 0x5f, 0x70, 0x29, 0x76, 0x72, 0xbd, 0x23,
	0x5f, 0xb8, 0x01, 0x1c, 0x50, 0x80, 0x1e, 0xe1,
	0x70, 0x55, 0xa5, 0xf5, 0x7f, 0x01, 0x57, 0x9b,
	0x60, 0xef, 0x01, 0x19, 0x0c, 0xc1, 0x01, 0x6f,
	0x39, 0xee, 0x01, 0x5c, 0x46, 0xb7, 0xd4, 0x47,
	0x68, 0xeb, 0xc5, 0x50, 0x32, 0x22, 0xe7, 0xcd,
	0x17, 0x16, 0x70, 0x3b, 0x76, 0x27, 0x21, 0x10,
	0x09, 0x42, 0x11, 0xf2, 0x06, 0xeb, 0x5b, 0xd4,
	0x10, 0x0f, 0x59, 0xdc, 0x1a, 0x27, 0x70, 0x9e,
	0x14, 0x33, 0x06, 0x24, 0x60, 0xb6, 0xdf, 0x70,
	0x68, 0xeb, 0x2f, 0x63, 0x1a, 0x32, 0x23, 0x4b,
	0x0a, 0x03, 0x82, 0xc5, 0x11, 0x44, 0x7b, 0x73,
	0x61, 0x3e, 0xfd, 0xc5, 0x7b, 0xd3, 0xee, 0x7b,
	0x5d, 0xc7, 0x9e, 0xa8, 0x1e, 0xba, 0x5d, 0x23,
	0x42, 0xc2, 0x96, 0xbd, 0x79, 0x9d, 0x02, 0x47,
	0x5f, 0x5e, 0x7f, 0xd0, 0x65, 0x90, 0x70, 0x0b,
	0x5d, 0xb7, 0x0a, 0xe5, 0x1f, 0x48, 0xea, 0xa1,
	0x1d, 0xba, 0xbf, 0x00, 0x2f, 0x30, 0x5d, 0xef,
	0x31, 0x0c, 0x50, 0xb3, 0x77, 0xae, 0x35, 0xeb,
	0x61, 0x57, 0x40, 0x95, 0x10, 0x23, 0x3c, 0x99,
	0x74, 0x12, 0x26, 0xbb, 0x35, 0x4f, 0xe9, 0xf9,
	0x23, 0x5b, 0xa8, 0x61, 0x1c, 0xf1, 0x0f, 0xd8,
	0x27, 0x5a, 0xc7, 0x94, 0x23, 0xf9, 0xc1, 0x3c,
	0x37, 0x4a, 0x3f, 0xe6, 0x70, 0xc6, 0xa5, 0x29,
	0x75, 0xc6, 0xc3, 0x3a, 0x1b, 0xa0, 0x26, 0xfa,
	0x74, 0x9a, 0xbb, 0x43, 0x68, 0x4a, 0x48, 0x1a,
	0x76, 0x44, 0xa4, 0x5c, 0x6d, 0xe9, 0x1b, 0x18,
	0x4b, 0x58, 0x8f, 0x54, 0x57, 0xe4, 0xcc, 0xaf,
	0x72, 0x5a, 0x06, 0xfb, 0x2a, 0x48, 0x7c, 0xb0,
	0x70, 0xa6, 0x4e, 0x2a, 0x15, 0x3e, 0xa4, 0x38,
	0x2d, 0x51, 0x77, 0x96, 0x5e, 0x88, 0x4a, 0xdc,
	0x5c, 0x48, 0x2a, 0x97, 0x77, 0x46, 0x5f, 0x01,
	0x05, 0x07, 0x23, 0x67, 0x55, 0x77, 0xf8, 0xe1,
	0x61, 0x4f, 0xd4, 0xa1, 0x45, 0x16, 0xdd, 0xe9,
	0x6c, 0xea, 0xf0, 0x87, 0x7c, 0x3d, 0xbd, 0x3d,
	0x08, 0x13, 0x86, 0x41, 0x02, 0x90, 0x1d, 0x82,
	0x2c, 0xa8, 0x86, 0x11, 0x54, 0xe4, 0x9e, 0xb4,
	0x0b, 0x03, 0xe0, 0xc6, 0x79, 0x83, 0x8c, 0xb2,
	0x75, 0xa2, 0xa8, 0xd4, 0x2d, 0x1d, 0x5a, 0xe9,
	0x72, 0x1d, 0xa3, 0x17, 0x62, 0x8c, 0x89, 0x5d,
	0x62, 0xbb, 0xd9, 0x5a, 0x7c, 0x83, 0xe4, 0x58,
	0x43, 0x1b, 0xd7, 0xb7, 0x25, 0xe4, 0x5d, 0x32,
	0x6b, 0x68, 0x07, 0x9a, 0x1b, 0xef, 0xd7, 0x9f,
	0x0d, 0xed, 0x72, 0x63, 0x33, 0x52, 0x25, 0x5a,
	0x66, 0xef, 0x43, 0x8d, 0x1f, 0x16, 0xe9, 0xe8,
	0x4d, 0xb1, 0x27, 0xf8, 0x5b, 0xd0, 0x62, 0xc2,
	0x75, 0x45, 0xe1, 0x46, 0x41, 0xb7, 0x1e, 0xfb,
	0x50, 0x7e, 0xd7, 0xab, 0x46, 0xe8, 0x7c, 0xcd,
	0x62, 0x55, 0x58, 0xec, 0x19, 0x49, 0x5c, 0xff,
	0x66, 0x33, 0x48, 0x73, 0x32, 0x7b, 0x23, 0xc6
};

static int
op_entry_cmp(op_entry *a, op_entry *b, int size)
{
	axdr_ret_t ret;
	axdr_state_t xdr;
	int len;
	char *buf;

	len = sizeof(op_entry_blob);
	buf = malloc(len);
	if (!buf) {
		return 1;
	}

	memset(buf, 0, len);
	axdrmem_create(&xdr, buf, len, AXDR_ENCODE);
	ret = axdr_op_entry(&xdr, a);
	axdr_destroy(&xdr);

	if (ret != AXDR_DONE || memcmp(buf, op_entry_blob, len) != 0) {
		free(buf);
		return 1;
	}

	memset(buf, 0, len);

	axdrmem_create(&xdr, buf, len, AXDR_ENCODE);
	ret = axdr_op_entry(&xdr, b);
	axdr_destroy(&xdr);

	if (ret != AXDR_DONE || memcmp(buf, op_entry_blob, len) != 0) {
		free(buf);
		return 1;
	}

	free(buf);
	return 0;
}

ATF_TC_BODY(test_xdr_pointers, tc)
{
	axdr_ret_t ret;
	axdr_state_t xdr;
	op_entry op_ent;
	int num_entries;

	/* test op_entry (pointers) */
	num_entries = sizeof(op_entry_blob) / sizeof(op_ent);
	memset(&op_ent, 0, sizeof(op_ent));

	axdrmem_create(&xdr, (char *) op_entry_blob,
		       sizeof(op_entry_blob), AXDR_DECODE);
	ret = axdr_op_entry(&xdr, &op_ent);
	ATF_REQUIRE(ret != AXDR_ERROR);
	if (ret == AXDR_DONE) {
		int j;

		axdr_test("axdr_op_entry", (axdrproc_t) axdr_op_entry,
			  &op_ent, num_entries * sizeof(op_ent),
			  (compare_t) op_entry_cmp);

		printf("\ntesting various record block sizes:\n");
		for (j = 100; j < 65000; ) {
			axdrrec_test_asyncrec((axdrproc_t) axdr_op_entry,
					      &op_ent,
					      num_entries * sizeof(op_ent),
					      (compare_t) op_entry_cmp,
					      2000, j, FALSE);
			axdrrec_test_asyncrec((axdrproc_t) axdr_op_entry,
					      &op_ent,
					      num_entries * sizeof(op_ent),
					      (compare_t) op_entry_cmp,
					      j, 2000, FALSE);
			if (j < 200) {
				j += 8;
			} else {
				j += j / 4;
			}
		}
	}
	axdr_free((axdrproc_t)axdr_op_entry, &op_ent);
	axdr_destroy(&xdr);
}


ATF_TC_WITHOUT_HEAD(test_xdr_serialize);

static void
axdr_test_deserialize(char *buf, size_t buflen,
		      axdrproc_t testfunc, void *expectedobj,
		      size_t size, compare_t cmp)
{
	axdr_state_t src;
	axdr_ret_t rval;
	void *dst;

	dst = calloc(1, size);
	ATF_REQUIRE(dst != NULL);
	memset(dst, 0, size);

	axdrmem_create(&src, buf, buflen, AXDR_DECODE);
	if (expectedobj == NULL) {
		ATF_REQUIRE_MSG((rval = testfunc(&src, dst)) == AXDR_ERROR,
				"unexpected return=%d",  rval);
		axdr_free(testfunc, dst);
		axdr_destroy(&src);
		free(dst);
		return;
	}

	ATF_REQUIRE_MSG((rval = testfunc(&src, dst)) == AXDR_DONE,
			"unexpected return=%d",  rval);
	ATF_REQUIRE_MSG(cmp(dst, expectedobj, size) == 0,
			"improper deserialization");
	axdr_free(testfunc, dst);
	axdr_destroy(&src);
	free(dst);
	return;
}

ATF_TC_BODY(test_xdr_serialize, tc)
{
	string_s str_s = { .str = __UNCONST("foobar") };
	test_u my_test_u = {
		.which = 8,
		.test_u_u = { .string_s_list = {
				 .string_s_list_len = 1,
				 .string_s_list_val = &str_s }
		}
	};
	char good_test_u_serialization[] =
	    "\x00\x00\x00\x08"
	    "\x00\x00\x00\x10"
	    "\x00\x00\x00\x01"
	    "\x00\x00\x00\x06"
	    "foobar\x00\x00";
	char bad_test_u_serialization[] =
	    "\x00\x00\x00\x08"
	    "\x00\x00\x00\x04"
	    "\x00\x00\x00\x01"
	    "\x00\x00\x00\x06"
	    "foobar\x00\x00";

	printf("\n(deserialize good serialization)\n");
	axdr_test_deserialize(good_test_u_serialization,
			      sizeof(good_test_u_serialization)-1,
			      (axdrproc_t) axdr_test_u, &my_test_u,
			      sizeof(test_u), (compare_t) test_u_cmp);
	printf("\n(deserialize bad serialization)\n");
	axdr_test_deserialize(bad_test_u_serialization,
			      sizeof(bad_test_u_serialization)-1,
			      (axdrproc_t) axdr_test_u, NULL,
			      sizeof(test_u), (compare_t) test_u_cmp);
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, test_xdr_types);
	ATF_TP_ADD_TC(tp, test_xdr_opaque);
	ATF_TP_ADD_TC(tp, test_xdr_serialize);
	ATF_TP_ADD_TC(tp, test_xdr_complex);
	ATF_TP_ADD_TC(tp, test_xdr_pointers);
	return atf_no_error();
}
